﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.SqlCommand;
using NHibernateMappingSample.Entities;
using ProjectBase.Data.NHibernate;


namespace NHibernateMappingSample.DataAccess
{
   public class SalesOrdersDao : GenericDao<SalesOrderHeader>
   {
      public IList<SalesOrderHeader> GetOrdersForCustomer(int customerID)
      {
           DetachedCriteria query = DetachedCriteria.For<SalesOrderHeader>();
            query.CreateAlias("BelongsToCustomer", "customer", JoinType.InnerJoin).
               Add(Expression.Eq("customer.CustomerID", customerID));
            ICriteria criteria = query.GetExecutableCriteria(Session);

            return criteria.List < SalesOrderHeader>();
      }

      public IList<SalesOrderHeader> GetOrderDetailsForCustomer(int customerID)
      {
         DetachedCriteria query = DetachedCriteria.For<SalesOrderHeader>();
         query.CreateAlias("BelongsToCustomer", "customer", JoinType.InnerJoin).
            CreateAlias("OrderDetails","orderDetails",JoinType.InnerJoin).
            Add(Expression.Eq("customer.CustomerID", customerID));
         //query.SetFetchMode("OrderDetails", FetchMode.Eager);

         ICriteria criteria = query.GetExecutableCriteria(Session);

         return criteria.List<SalesOrderHeader>();
      }
   }
}
